#include "SystemRoot.h"

#include "server/misc/Config.h"
#include "misc/Event.h"
#include "misc/Interface.h"
#include "server/net/NetworkControl.h"
#include "server/Server.h"
#include "server/file/FileControl.h"
#include "misc/Debug.h"

#include <Poco/Net/SocketAddress.h>

SystemRoot* SystemRoot::sysRoot = 0;		// Set the static pointer
/*********************************************************/
void SystemRoot::start(const std::string& name)
{
	Config& cfg = Config::Instance();
	cfg.setRingName(name);
	
	start();
}
/*********************************************************/
void SystemRoot::start()
{
	Config& cfg = Config::Instance();			// Start the config singleton, parsing the configuration file

	// Retrieve the instances
	Interface& iface = Interface::Instance();		// Get the interface singleton
	NetworkControl& netCtrl = NetworkControl::Instance();	// Get the middleware instance
	FileControl& fCtrl = FileControl::Instance();		// Get the file control instance

	// Start them
	iface.start(cfg.getInterfaceInputEvent());
	iface << "Starting Network Controller"; iface.flush();
	netCtrl.start();
	iface << "... done" << std::endl << "Starting File System"; iface.flush();
	fCtrl.init();
	iface << "... done" << std::endl << "Starting Server Framework"; iface.flush();
	server.start();
	iface << "... done" << std::endl; iface.flush();
}
/*********************************************************/
void SystemRoot::stop()
{
	// Retrieve the instances
	Interface& iface = Interface::Instance();		// Get the interface singleton
	NetworkControl& netCtrl = NetworkControl::Instance();	// Get the middleware instance
	FileControl& fCtrl = FileControl::Instance();		// Get the filecontrol instance
	Config& cfg = Config::Instance();			// Get the configuration instance
	Debug& DEBUG = Debug::Instance();			// Get the debug instance

	// Start them
	iface << "Stopping Server Framework"; iface.flush();
	server.stop();
	iface << "... done" << std::endl << "Stopping Network Controller"; iface.flush();
	netCtrl.stop();
	iface << "... done" << std::endl << "Stopping File System"; iface.flush();
	fCtrl.deinit();
	iface << "... done" << std::endl;
	iface.stop();

	// Do we need to write a debug report
	if ( cfg.getWriteDumpLog() != "" ) {
		DEBUG.log(cfg.getWriteDumpLog());
	}
}
/*********************************************************/
void SystemRoot::run()
{
	// Get a configuration instance & the quit event singleton
	Config& cfg = Config::Instance();
	Event* event = Event::Instance(cfg.getQuitEvent());

	event->wait();		// Wait for a quit flag...
}
/*********************************************************/
SystemRoot::SystemRoot()
{
	// Boot the config singleton & let it try to parse config.ini, if unavailable it will write a default config.ini and parse it
	Config& cfg = Config::Instance();

	// Set the correct Event strings in the config class
	cfg.setUnicastTrafficEvent("NETTRAFFIC");
	cfg.setMulticastTrafficEvent("NETTRAFFIC");
	cfg.setActivateProcessingEvent("PROCESSEVENT");
	cfg.setInterfaceInputEvent("PROCESSEVENT");
	cfg.setQuitEvent("STOP");
	
	// Get the singleton instances
	Interface& iface = Interface::Instance();		// Get the interface singleton
	NetworkControl::Instance();				// Get the middleware instance

	if ( sysRoot == 0 ) {
		sysRoot = this;
	} else {
		if ( sysRoot != this ) {
			iface << "Starting multiple instances of SystemRoot will cause unspected server behaviour!" << std::endl;
		}
	}
}
/*********************************************************/
SystemRoot::~SystemRoot()
{
}
